<%--
  Created by IntelliJ IDEA.
  User: 74018
  Date: 2019/1/7
  Time: 16:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加订单</title>
    <jsp:include page="${pageContext.request.contextPath}/common/common.jsp"/>
</head>
<body>
<div class="container">
    <div class="container">
        <table id="tb"></table>
    </div>
</div>
<!-- Button trigger modal -->

<form action="#" id="myTool" class="form-inline">
    <div class="form-group">
        <label for="gid">编号</label>
        <input type="text" class="form-control" id="gid" name="gid" disabled>
    </div>
    <div class="form-group">
        <label for="gName">客户</label>
        <input type="text" class="form-control" id="gName" disabled>
    </div>
    <div class="form-group">
        <label for="gcarBrnad">车辆</label>
        <input type="text" class="form-control" id="gcarBrnad" name="carBrand" disabled>
    </div>
    <div class="form-group">
        <label for="gcarModel">车牌</label>
        <input type="text" class="form-control" id="gcarModel" name="carModel" disabled>
    </div>
    <div class="form-group">
        <label for="staff">维修员工</label>
        <select class="form-control" id="staff" name="staff">
        </select>
    </div>
    <div class="form-group">
        <input type="button" class="btn-primary btn" value="生成订单" onclick="addOrder();">
    </div>
    <div class="form-group">
        <input type="button" class="btn-primary btn" value="返回" onclick="location.href='http://localhost:8088/repair/guestsCar.jsp'">
    </div>
</form>


<!-- Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modelTitleId">订单确认</h4>
            </div>
            <div class="modal-body">
                <table id="orderInfoTb"></table>

                <div class="row" style="margin: 5px"><textarea id="note" class="form-control" placeholder="备注信息" style="resize:none" rows="3"></textarea></div>
                <div class="row"style="margin: 5px"><h3 >价格:<label id="money"style="color:red"></label></h3>
                <input type="hidden" name="money" /></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="doAdd();">确定</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script>
    $(function () {
        $("#tb").bootstrapTable({
            url:"${pageContext.request.contextPath}/repairOrder/query",
            toolbar:'#myTool',//工具按钮用哪个容器
            striped: true,//隔行换色
            //showRefresh: true,//是否显示刷新按钮
            clickToSelect: true,
            height: 500,//行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",//每一行的唯一标识，一般为主键列
            //showToggle:true,//是否显示详细视图和列表视图的切换按钮
            pagination:true, //是否显示分页（*）
            pageSize : 10,//单页记录数
            pageList : [ 10, 20, 30 ],//可选择单页记录数
            columns:[{
                checkbox:true,
            },{
                field:'id',
                title:'编号'
            },{
                field:'name',
                title:'名称'
            },{
                field:'price',
                title:'价格'
            },{
                field:'note',
                title:'备注信息'
            }]
        });

        initSelect();
        $("#gName").val("${g.name}");
        $("[name=carModel]").val("${c.carModel}");
        $("[name=gid]").val("${c.gid}");
        $("[name=carBrand]").val("${c.carBrand}");

        $("#orderInfoTb").bootstrapTable({
            height: 300,
            columns:[{
                checkbox:true
            },{
                field:'id',
                title:'编号'
            },{
                field:'name',
                title:'名称'
            },{
                field:'price',
                title:'价格'
            }]
        });

    });
    function initSelect() {
        $("#staff option").remove();
        $.post("${pageContext.request.contextPath}/user/getUserByRole",{"oid":3},function (data) {
            $.each(data,function (index,item) {
                $("#staff").append('<option value='+item.id+'>'+item.workname+'</option>');
            });
        });
    }
    function addOrder() {
        var rows = $("#tb").bootstrapTable("getSelections") ;
        if (rows.length<1){
            swal("提示信息", "请选择数据!", "error");
        } else{
            $('#orderInfoTb').bootstrapTable("removeAll");
            $("#addModal").modal("show");
            console.log(rows);
            var money = 0;
            $.each(rows, function (index,ele) {
                $('#orderInfoTb').bootstrapTable('insertRow', {
                    index:$('#orderInfoTb').bootstrapTable('getOptions').totalRows,
                    row:rows[index]
                });
                money += ele.price;
            })
            $("#money").text(money);
            $("[name=money]").val(money);
        }
    }

    function doAdd() {
        var moeny = $("[name=money]").val();
        var note = $("#note").val();
        var gid = $("#gid").val();
        var staff = $("#staff").val();
        var orId = $("[name=carBrand]").val();
        var gcar = ${c.id};
        var rows = $("#orderInfoTb").bootstrapTable('getAllSelections');
        var rsId = "";
        $.each(rows, function (index,item) {
            rsId += item.id+"-";
        })
        console.log(rsId);
        console.log(moeny);
        $.post("${pageContext.request.contextPath}/repairOrder/addOrder",{'gId':gid,'staff':staff,'moeny':moeny,'note':note,'gcar':gcar,'orinfoId':rsId},function (data) {
            console.log(data);
            if (data>0){
                swal("提示信息！", "添加成功", "success");
            } else{
                swal("提示信息！", "添加失败", "error");
            }
        })
    }

</script>
